<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>自动配置路由 | MyUI 4.x</title>
    <meta name="generator" content="VuePress 1.5.4">
    <link rel="icon" href="/my/favicon.ico">
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?c4e5f73318b5cb0c389e3d9a05f831cc";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
    <meta name="description" content="MyUI是美亚柏科旗下新德汇出品的Web前端一站式项目工程框架，专注于公检法行业中台系统快速搭建，框架已在多个项目实战检验。">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <link rel="preload" href="/my/assets/css/0.styles.810d4cba.css" as="style"><link rel="preload" href="/my/assets/js/app.2dbd0b81.js" as="script"><link rel="preload" href="/my/assets/js/493.009afff8.js" as="script"><link rel="preload" href="/my/assets/js/1358.c6f8b936.js" as="script">
    <link rel="stylesheet" href="/my/assets/css/0.styles.810d4cba.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/my/" class="home-link router-link-active"><img src="/my/img/logo.png" alt="MyUI 4.x" class="logo"> <span class="site-name can-hide">MyUI 4.x</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/my/guide/" class="nav-link router-link-active">
  指南
</a></div><div class="nav-item"><a href="/my/ui/components/" class="nav-link">
  组件
</a></div><div class="nav-item"><a href="/my/ui/charts/" class="nav-link">
  图表
</a></div><div class="nav-item"><a href="/my/ui/map/" class="nav-link">
  地图
</a></div><div class="nav-item"><a href="/my/ui/go/" class="nav-link">
  关系图
</a></div><div class="nav-item"><a href="/my/ui/icon/" class="nav-link">
  图标
</a></div><div class="nav-item"><a href="/my/ui/pages/" class="nav-link">
  页面
</a></div><div class="nav-item"><a href="http://newgateway.gitee.io/my-web/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="JSDoc" class="dropdown-title"><span class="title">JSDoc</span> <span class="arrow down"></span></button> <button type="button" aria-label="JSDoc" class="mobile-dropdown-title"><span class="title">JSDoc</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/my/api/components.html" class="nav-link">
  基础组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/utils.html" class="nav-link">
  工具函数
</a></li><li class="dropdown-item"><!----> <a href="/my/api/charts.html" class="nav-link">
  图表组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/map.html" class="nav-link">
  地图组件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/my/guide/" class="nav-link router-link-active">
  指南
</a></div><div class="nav-item"><a href="/my/ui/components/" class="nav-link">
  组件
</a></div><div class="nav-item"><a href="/my/ui/charts/" class="nav-link">
  图表
</a></div><div class="nav-item"><a href="/my/ui/map/" class="nav-link">
  地图
</a></div><div class="nav-item"><a href="/my/ui/go/" class="nav-link">
  关系图
</a></div><div class="nav-item"><a href="/my/ui/icon/" class="nav-link">
  图标
</a></div><div class="nav-item"><a href="/my/ui/pages/" class="nav-link">
  页面
</a></div><div class="nav-item"><a href="http://newgateway.gitee.io/my-web/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  演示
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="JSDoc" class="dropdown-title"><span class="title">JSDoc</span> <span class="arrow down"></span></button> <button type="button" aria-label="JSDoc" class="mobile-dropdown-title"><span class="title">JSDoc</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/my/api/components.html" class="nav-link">
  基础组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/utils.html" class="nav-link">
  工具函数
</a></li><li class="dropdown-item"><!----> <a href="/my/api/charts.html" class="nav-link">
  图表组件
</a></li><li class="dropdown-item"><!----> <a href="/my/api/map.html" class="nav-link">
  地图组件
</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>开发指南</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/my/guide/access.html" class="sidebar-link">权限控制</a></li><li><a href="/my/guide/app.html" class="sidebar-link">子应用微服务</a></li><li><a href="/my/guide/cmd.html" class="sidebar-link">脚本命令</a></li><li><a href="/my/guide/coder.html" class="sidebar-link">代码生成器</a></li><li><a href="/my/guide/config.html" class="sidebar-link">配置</a></li><li><a href="/my/guide/constant.html" class="sidebar-link">常量和静态配置</a></li><li><a href="/my/guide/extension.html" class="sidebar-link">内置实例扩展</a></li><li><a href="/my/guide/route.html" aria-current="page" class="active sidebar-link">自动配置路由</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my/guide/route.html#命名规范" class="sidebar-link">命名规范</a></li><li class="sidebar-sub-header"><a href="/my/guide/route.html#特殊的页面命名" class="sidebar-link">特殊的页面命名</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my/guide/route.html#index-vue" class="sidebar-link">index.vue</a></li><li class="sidebar-sub-header"><a href="/my/guide/route.html#_404-vue" class="sidebar-link">_404.vue</a></li><li class="sidebar-sub-header"><a href="/my/guide/route.html#_404-vue-2" class="sidebar-link">404.vue</a></li><li class="sidebar-sub-header"><a href="/my/guide/route.html#layout-vue" class="sidebar-link">_layout.vue</a></li><li class="sidebar-sub-header"><a href="/my/guide/route.html#以-开头的文件" class="sidebar-link">以_开头的文件</a></li><li class="sidebar-sub-header"><a href="/my/guide/route.html#以-开头的文件-2" class="sidebar-link">以$开头的文件</a></li></ul></li><li class="sidebar-sub-header"><a href="/my/guide/route.html#定义路由元信息" class="sidebar-link">定义路由元信息</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/my/guide/route.html#keepalive" class="sidebar-link">keepAlive</a></li></ul></li><li class="sidebar-sub-header"><a href="/my/guide/route.html#路由守卫" class="sidebar-link">路由守卫</a></li><li class="sidebar-sub-header"><a href="/my/guide/route.html#手工编写路由表" class="sidebar-link">手工编写路由表</a></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>内置资源</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>进阶教程</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>第三方类库</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="自动配置路由"><a href="#自动配置路由" class="header-anchor">#</a> 自动配置路由</h1> <p><code>MyWeb</code> 4.x 开始支持自动生成路由配置表。在开发模式时会自动检测 <code>views</code> 目录的文件变化，自动生成对应的页面路由。无需手工配置。
自动生成路由是默认开启的，你可以通过配置<a href="/my/guide/config.html#autoroutes">关闭这个功能</a>。</p> <p>自动生成的路由配置文件存放在 <code>/.my/routes/</code> 文件夹下</p> <h2 id="命名规范"><a href="#命名规范" class="header-anchor">#</a> 命名规范</h2> <p><code>views</code> 文件夹下只能放置文件夹或<code>.vue</code>文件。命名必须要全小写，多个单词用<code>-</code>分隔，如：<code>user-info</code></p> <h2 id="特殊的页面命名"><a href="#特殊的页面命名" class="header-anchor">#</a> 特殊的页面命名</h2> <p>有几个固定命名的文件，有特定意义和功能，包括： <code>index.vue</code>、 <code>_404.vue</code>、<code>_layout.vue</code></p> <h3 id="index-vue"><a href="#index-vue" class="header-anchor">#</a> index.vue</h3> <p>首页或目录的首页，解析器在遇到这个文件时会生成一个命名为空字符串的路由项。</p> <p>如：<code>/views/index.vue</code> 生成的路由配置</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span>
  get
<span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'index'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    meta<span class="token operator">:</span> <span class="token keyword">null</span>
  <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="_404-vue"><a href="#_404-vue" class="header-anchor">#</a> _404.vue</h3> <p>当路由没有匹配到规则时展示的页面。</p> <p>如：<code>/views/_404.vue</code> 生成的路由配置</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span>
  get
<span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'index'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    meta<span class="token operator">:</span> <span class="token keyword">null</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">'/*'</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'_404'</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_404-vue-2"><a href="#_404-vue-2" class="header-anchor">#</a> 404.vue</h3> <p>当路由没有匹配到规则时展示的页面。
与 _404.vue的区别是，_404.vue不放入子路由，而404.vue放入子路由。</p> <h3 id="layout-vue"><a href="#layout-vue" class="header-anchor">#</a> _layout.vue</h3> <p>如果页面有公共的部分，需要用多级路由来实现。需要在当前目录下创建 <code>_layout.vue</code>。 当一个文件存在 <code>_layout.vue</code>时，
该文件夹下的vue文件（除了<code>_404.vue</code>、以<code>_</code>命名开头的文件）都加入到子路由。</p> <div class="custom-block warning"><p class="custom-block-title">WARNING</p> <p>_layout.vue 文件需要包含 <code>router-view</code> 才能加载路由页面</p></div> <p>如： 新增 <code>/views/_layout.vue</code> 生成的路由配置</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span>
  get
<span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">'/'</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'_layout'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    children<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
      path<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
      component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'index'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      meta<span class="token operator">:</span> <span class="token keyword">null</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">'/*'</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'_404'</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h3 id="以-开头的文件"><a href="#以-开头的文件" class="header-anchor">#</a> 以<code>_</code>开头的文件</h3> <p>如果其中有些同级的页面希望不加入子路由中，可以命名以<code>_</code>开头即可。
生成的路由<code>path</code> 不包含开头的<code>_</code>字符</p> <p>例如登录页面，通常是独立的页面，不需要包含公共部分</p> <p>新增 <code>/views/_login.vue</code> 生成的路由配置</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span>
  get
<span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">'/'</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'_layout'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    children<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
      path<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
      component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'index'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      meta<span class="token operator">:</span> <span class="token keyword">null</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">'/login'</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'_login'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    meta<span class="token operator">:</span> <span class="token keyword">null</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">'/*'</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'_404'</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="以-开头的文件-2"><a href="#以-开头的文件-2" class="header-anchor">#</a> 以<code>$</code>开头的文件</h3> <p>当路由path是动态参数时，文件名需要以<code>$</code>开头。
例如：访问路由需要传递参数名为id的页面，新建文件 <code>/views/users/$id.vue</code> 生成的路由配置</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span>
  get
<span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">'/'</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'_layout'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    children<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
      path<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
      component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'index'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      meta<span class="token operator">:</span> <span class="token keyword">null</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
      path<span class="token operator">:</span> <span class="token string">'/users/:id'</span><span class="token punctuation">,</span>
      component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'users/$id'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      meta<span class="token operator">:</span> <span class="token keyword">null</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">'/login'</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'_login'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    meta<span class="token operator">:</span> <span class="token keyword">null</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    path<span class="token operator">:</span> <span class="token string">'/*'</span><span class="token punctuation">,</span>
    component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'_404'</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h2 id="定义路由元信息"><a href="#定义路由元信息" class="header-anchor">#</a> 定义路由元信息</h2> <p>路由原型可以通过 <code>Front Matter</code> 的方式定义。
front matter 必须是 vue 文件中的第一部分，并且必须采用在三点划线之间书写的有效的 YAML。 这是一个基本的例子：</p> <div class="language-md line-numbers-mode"><pre class="language-md"><code><span class="token hr punctuation">---</span>
title: Blogging Like a Hacker
<span class="token title important">lang: en-US
<span class="token punctuation">---</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="keepalive"><a href="#keepalive" class="header-anchor">#</a> keepAlive</h3> <p>工程预设了 keepAlive 可以变量，用来控制当前路由加载的页面组件是否需要保持 keepAlive</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>---
keepAlive: true
---
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    Index
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
  <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>


</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="路由守卫"><a href="#路由守卫" class="header-anchor">#</a> 路由守卫</h2> <p><code>Access</code>提供了路由守卫的功能，可用 <code>Front Matter</code> 的方式或自定义函数实现，详情请参考 <a href="/my/guide/access.html#路由守卫">Access的路由守卫实现</a></p> <h2 id="手工编写路由表"><a href="#手工编写路由表" class="header-anchor">#</a> 手工编写路由表</h2> <p>自动配置路由的功能默认开启，如需手动编写路由，可配置 <code>/src/config.js</code></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span><span class="token keyword">set</span> <span class="token keyword">as</span> setConfig<span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'$ui/config'</span>

<span class="token comment">// 写入运行时配置</span>
<span class="token function">setConfig</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token comment">// 关闭自动生成路由配置</span>
  autoRoutes<span class="token operator">:</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>当关闭自动配置路由时，你需要自行编写路由配置，文件：<code>/src/router/routes.js</code></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span>get<span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
      path<span class="token operator">:</span> <span class="token string">'/path'</span><span class="token punctuation">,</span>
      component<span class="token operator">:</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'path'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><code>get</code>方法传入相对<code>views</code>目录的文件路径</p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/my/guide/extension.html" class="prev">
        内置实例扩展
      </a></span> <span class="next"><a href="/my/guide/color.html">
        色彩
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/my/assets/js/app.2dbd0b81.js" defer></script><script src="/my/assets/js/493.009afff8.js" defer></script><script src="/my/assets/js/1358.c6f8b936.js" defer></script>
  </body>
</html>
